package com.sunshine.schedulegrid.view

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import com.sunshine.schedulegrid.config.ScheduleConfig
import com.sunshine.schedulegrid.utils.DateUtils
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList
import kotlin.collections.HashMap

/**
 * @author sunshine
 * @date 2020/8/29
 * @param sc 日程格子配置对象
 * @param thisDateLong 这周日期的long值
 * 上方日期栏布局
 */
internal class DateView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
    sc: ScheduleConfig,
    private val thisDateLong: Long = Calendar.getInstance().timeInMillis
) : LinearLayout(context, attrs, defStyleAttr) {

    private var scheduleConfig: ScheduleConfig = sc

    //这个view为第几周
    private val thisWeek = DateUtils.getThisWeek(thisDateLong)

    init {
        initButton()
    }

    /**
     * 初始化日期栏按钮
     */
    @SuppressLint("SimpleDateFormat")
    private fun initButton() {
        //月份指示
        val timeButton = Button(context)
        val simpleDateFormat = SimpleDateFormat("M月")
        timeButton.apply {
            text = simpleDateFormat.format(Date(thisDateLong))
            textSize = scheduleConfig.dateButtonTextSize()
            setBackgroundColor(Color.TRANSPARENT)
        }

        val buttonParams = LayoutParams(0, LayoutParams.MATCH_PARENT, 1f)
        val timeParams = LayoutParams(scheduleConfig.timeWidth(), LayoutParams.MATCH_PARENT)

        addView(timeButton, timeParams)

        //日期栏按钮
        val dateButtonMap = HashMap<Int, Button>()

        //日期对象，用于日期显示
        val cal = Calendar.getInstance().apply { timeInMillis = thisDateLong }
        //今天的日期对象
        val calNow = Calendar.getInstance().apply { timeInMillis = DateUtils.getNowDateLong() }
        //日期格式化
        simpleDateFormat.applyPattern("EE\nM/d")

        //今天是一周的第几天，用来进行日期推算
        val todayOfWeek = DateUtils.getDayOfWeek(cal[Calendar.DAY_OF_WEEK])
        for (i in 1 .. scheduleConfig.showDaysCount()){
            //推算到当天，注意最后要推算回去
            cal[Calendar.DAY_OF_MONTH] += (i - todayOfWeek)
            dateButtonMap[i] = Button(context).apply {
                text = simpleDateFormat.format(cal.time)

                 //设置今天为高亮
                if (cal[Calendar.DAY_OF_MONTH] == calNow[Calendar.DAY_OF_MONTH] && cal[Calendar.MONTH] == calNow[Calendar.MONTH]) {
                    setBackgroundColor(Color.YELLOW)
                } else{
                    setBackgroundColor(Color.TRANSPARENT)
                }
            }
            addView(dateButtonMap[i], buttonParams)

            //推算回“今天”的日期
            cal[Calendar.DAY_OF_MONTH] -= (i - todayOfWeek)
        }
//        dateButtonList.forEach {
//            it.apply {
//                text = simpleDateFormat.format(Date(DateUtils.getNowDateLong()))
//                textSize = scheduleConfig.dateButtonTextSize()
//                setBackgroundColor(Color.TRANSPARENT)
//            }
//            addView(it, buttonParams)
//        }
    }
}